import 'package:flutter/material.dart';

class MessageDialog extends Dialog{

  String title;
  String message;
  String negativeText;
  String positiveText;
  Function onCloseEvent;
  Function onPositiveEvent;

  MessageDialog({
    Key key,
    @required this.title,
    @required this.message,
    this.negativeText,
    this.positiveText,
    this.onCloseEvent,
    @required this.onPositiveEvent
  }):super(key: key);


  @override
  Widget build(BuildContext context) {
    return new Padding(
      padding: const EdgeInsets.all(15.0),
      child: new Material(
        type: MaterialType.transparency,
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Container(
              decoration: ShapeDecoration(
                color: Color(0xffffffff),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(8.0),
                  ),
                ),
              ),
              margin: const EdgeInsets.all(12.0),
              child: new Column(
                children: <Widget>[
                  new Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: new Stack(
                      alignment: AlignmentDirectional.centerEnd,
                      children: <Widget>[
                        new Center(
                          child: new Text(
                            title,
                            style: new TextStyle(
                              fontSize: 19.0,
                            ),
                          ),
                        ),
                        new GestureDetector(
                          onTap: this.onCloseEvent,
                          child: new Padding(
                            padding: const EdgeInsets.all(5.0),
                            child: new Icon(
                              Icons.close,
                              color: Color(0xffe0e0e0),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  new Container(
                    color: Color(0xffe0e0e0),
                    height: 1,),
                  new Container(
                    constraints: BoxConstraints(minHeight: 180.0),
                    child: new Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: new IntrinsicHeight(
                        child: new Text(
                          message,
                          style: TextStyle(fontSize: 16.0),
                        ),
                      ),
                    ),
                  ),
                  this._buildBottomButtonGroup(),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildBottomButtonGroup() {
    var widgets = <Widget>[];
    if(negativeText != null && negativeText.isNotEmpty) widgets.add(_buildCancelButton());
    if(positiveText != null && positiveText.isNotEmpty) widgets.add(_buildPositiveButton());
    return new Flex(
      direction: Axis.horizontal,
      children: widgets,
    );
  }

  Widget _buildCancelButton(){
    return new Flexible(
      fit: FlexFit.tight,
      child: new FlatButton(
          onPressed: onCloseEvent,
          child: new Text(negativeText,
            style: TextStyle(fontSize: 16.0),
          )
      ),
    );
  }

  Widget _buildPositiveButton(){
    return new Flexible(
        fit: FlexFit.tight,
        child: new FlatButton(
            onPressed: onPositiveEvent,
            child: new Text(
              positiveText,
              style: TextStyle(
                color: Color(Colors.teal.value),
                fontSize: 16.0,
              ),
            )));
  }
}